package com.xbongbong.rollback.service;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.pojo.dto.PhysicalDeleteDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.domain.entity.ReRollBackEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 表单操作回滚 接口
 * @author 吴峰
 * @date 2020/12/31 16:29
 * @since
 * @version
 */
@Service("formDataUpdateRollBackService")
public abstract class AbstractFormDataUpdateRollBackService {

    @Resource
    private PaasEsModel paasEsModel;



    /**
     * 更新的回滚
     * @param newPaasFormDataEntity
     * @param oldPaasFormData
     * @param dataId
     * @param corpid
     * @param saasMark
     * @param businessType
     * @param reRollBackEntity
     * @return void
     * @author 吴峰
     * @date 2021/2/25 16:20
     * @throws XbbException       
     */
    public abstract void rollBack4Update(PaasFormDataEntity newPaasFormDataEntity, PaasFormDataEntity oldPaasFormData, Long dataId, String corpid, Integer saasMark, Integer businessType, ReRollBackEntity reRollBackEntity);



    /**
     * Description: 主业务数据物理删除
     * @param dataId 数据id
     * @param corpid 公司id
     * @param indexTypeEnum es索引枚举
     * @see IndexTypeEnum
     * @author 魏荣杰
     * @date 2020/12/31 17:10
     */
    protected void physicalDeleteFormData(Long dataId, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException {
        PhysicalDeleteDTO physicalDeleteDTO = new PhysicalDeleteDTO();
        physicalDeleteDTO.setEsId(corpid + "_" + dataId);
        physicalDeleteDTO.setIndex(indexTypeEnum.getIndex());
        physicalDeleteDTO.setType(indexTypeEnum.getType());
        paasEsModel.physicalDelete(physicalDeleteDTO);
    }


    /**
     * Description: 物理删除子表单数据
     * @param corpid
     * @param subDataEntities
     * @param indexTypeEnum
     * @return void
     * @throws
     * @author 魏荣杰
     * @date 2020/9/30 15:35
     * @since
     */
    protected void physicalDeleteSubBatch(String corpid, List<? extends PaasFormSubDataEntity> subDataEntities, IndexTypeEnum indexTypeEnum) throws XbbException {
        List<DeleteDTO> deleteSubList = new ArrayList<>();
        for (PaasFormSubDataEntity paasFormSubDataEntity : subDataEntities) {
            DeleteDTO deleteDTO = new DeleteDTO();
            deleteDTO.setEsId(corpid + "_" + paasFormSubDataEntity.getId());
            deleteDTO.setIndexTypeEnum(indexTypeEnum);
            deleteDTO.setParent(corpid + "_" + paasFormSubDataEntity.getDataId());
            deleteSubList.add(deleteDTO);
        }
        paasEsModel.physicalDeleteBatchChild(deleteSubList, corpid, indexTypeEnum);
    }

}
